package com.rs.portal.doctor.wap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.jaxplat.commons.Row;
import com.jaxplat.commons.RowImpl;
import com.rs.commons.RsConstant;
import com.rs.commons.delegate.CommonDelegate;
import com.rs.commons.web.PortalBaseAction;
import com.rs.rsbiz.doctor.delegate.DoctorDelegate;
import com.rs.util.Pagination;
import com.rs.util.Utils;

public class DoctorAction extends PortalBaseAction
{
    private DoctorDelegate doctorDelegate;

    private CommonDelegate commonDelegate;

    public CommonDelegate getCommonDelegate()
    {
        return commonDelegate;
    }

    public void setCommonDelegate(CommonDelegate commonDelegate)
    {
        this.commonDelegate = commonDelegate;
    }

    public DoctorDelegate getDoctorDelegate()
    {
        return doctorDelegate;
    }

    public void setDoctorDelegate(DoctorDelegate doctorDelegate)
    {
        this.doctorDelegate = doctorDelegate;
    }

    /**
     * 查询第一步 初始化医院信息
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initSearch_s1(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Map<String, Object> attrs = new HashMap<String, Object>();
        Row params = this.parseRequestParametersToRow(request);
        String cityCode = "";
        String currCityCode = RsConstant.DEFAULT_CITY_CODE;
        String currCityName = RsConstant.DEFAULT_CITY_NAME;
        cityCode = params.getString("city_code");
          if(cityCode==null||"".equals(cityCode)){
           	cityCode=(String) request.getSession().getAttribute("area");
          }
          if(cityCode==null||"".equals(cityCode)){
          cityCode = RsConstant.DEFAULT_CITY_CODE;
        }
        
        Row cityInfo = commonDelegate.getCity(cityCode);
        if (null != cityInfo)
        {
            currCityCode = cityInfo.getString("city_code");
            currCityName = cityInfo.getString("city_name");
        }
        else
        {
            currCityCode = RsConstant.DEFAULT_CITY_CODE;
            currCityName = RsConstant.DEFAULT_CITY_NAME;
        }

        List<?> hosList = commonDelegate.getAllHospitalOfCity(cityCode);
        request.getSession().setAttribute("curr_city_code", currCityCode);
        request.getSession().setAttribute("curr_city_name", currCityName);

        attrs.put("list", hosList); //
        // 这一句相当于request.setAttribute("list",list);
        // attrs.put("command", new DoctorEntity());
        // attrs.put("curDate", Utils.getCurDate("yyyy-MM-dd"));
        return new ModelAndView("doctor_search_s1", attrs);

    }

    /**
     * 初始化查询页面
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView initSearch_s2(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Map<String, Object> attrs = new HashMap<String, Object>();
        // 设置下拉框的值
        Row param = this.parseRequestParametersToRow(request);
        initOptions(param, attrs);
        return new ModelAndView("doctor_search", attrs);

    }

    @SuppressWarnings("unchecked")
	private void initOptions(Row params, Map<String, Object> attrs) throws Exception
    {
        List<Row> secList = commonDelegate.getAllSectionWithParentList(params);
        String dateStr = Utils.getCurAfterDate("yyyy-MM-dd", 1);
		String hId = "";
        String sId = "";
        if (!Utils.isNullOrBlank(params.getString("hospital_id")))
            hId = params.getString("hospital_id");
        if (!Utils.isNullOrBlank(params.getString("section_id")))
            sId = params.getString("section_id");
        String secOptionsHtml = Utils.createOptionsHtml(secList, "id", "ps_name", sId, "", "所有科室", true);
        attrs.put("secOptions", secOptionsHtml);
        if (!Utils.isNullOrBlank(params.getString("date")))
        {
            dateStr = params.getString("date");
        }
        // 创建日期列表
        List dateList = new ArrayList();
        Row row = null;
        for (int i = 1; i < RsConstant.PRE_SCHEDULE_DAYS; i++)
        {
            row = new RowImpl();
            row.addColumn("key", Utils.getCurAfterDate("yyyy-MM-dd", i));
            row.addColumn("val", Utils.getCurAfterDate("yyyy-MM-dd", i));
            dateList.add(row);
        }
        String dateOptionsHtml = Utils.createOptionsHtml(dateList, "key", "val", dateStr, "", "", false);
        attrs.put("dateOptions", dateOptionsHtml);
        attrs.put("date", dateStr);
        attrs.put("hospital_name", commonDelegate.getHospitalNameById(hId));
        attrs.put("hospital_id", hId);
    }

    /**
     * 专家查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
	public ModelAndView searchDoc(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map<String, Object> attrs = new HashMap<String, Object>();
        // 设置下拉框的值
        initOptions(param, attrs);

        int pageIndex = 1;
        /*if (param.getInt("pageIndex") != 0)
        {
            pageIndex = param.getInt("pageIndex");
        }*/
        if (param.getInt("page") != 0)
        {
            pageIndex = param.getInt("page");
        }


        param.put("pageIndex", pageIndex);
        param.put("pageSize", "10");
        // param.put("pageIndex", "1");

        Object[] result = this.getDoctorDelegate().getDocList(param);
        List list = (List) result[0];
        Integer totolCount = (Integer) result[1];

        int pageCount = totolCount / 10;
        if (totolCount % 10 > 0)
        {
            pageCount++;
        }
        StringBuffer pageLinks = new StringBuffer();
        String requestUrl = request.getRequestURL().toString() + "?method=searchDoc";
        String urlParams = this.createDocSearchLinkParams(param);
        /*for (int i = 1; i <= pageCount; i++)
        {
            if (i == pageIndex)
            {
                String href = "&nbsp;[" + i + "]";
                pageLinks.append(href);
            }
            else
            {
                String url = requestUrl + urlParams + "&pageIndex=" + i;
                String href = "&nbsp;[<a href='" + url + "' class='back'>" + i + "</a>]";
                pageLinks.append(href);
            }
        }*/
        String pagingHtml;
        Pagination pagination = new Pagination();
        String action = requestUrl + urlParams;
        pagingHtml = pagination.getPageTag(totolCount, RsConstant.PAGESIZE, pageIndex, action);

        //pagingHtml = pagination.getPageTag(pageCount, RsConstant.PAGESIZE, pageIndex, requestUrl);
        
//        for (int i = 1; i <= pageCount; i++)
//        {
//            if (i == pageIndex)
//            {
//                String href = "&nbsp;[" + i + "]";
//                pageLinks.append(href);
//            }
//            else
//            {
//                String url = requestUrl + urlParams + "&pageIndex=" + i;
//                String href = "&nbsp;[<a href='" + url + "' class='back'>" + i + "</a>]";
//                pageLinks.append(href);
//            }
//        }

        attrs.put("list", list);
        attrs.put("hospital_id", param.getString("hospital_id"));
        attrs.put("name", param.getString("name"));
        //attrs.put("pageLinks", pageLinks.toString());
        attrs.put("pageLinks", pagingHtml);


        return new ModelAndView("doctor_search", attrs);
    }

    private String createDocSearchLinkParams(Row params)
    {
        StringBuffer linkParam = new StringBuffer();
        if (!Utils.isNullOrBlank(params.getString("hospital_id")))
        {
            linkParam.append("&hospital_id=" + params.getString("hospital_id"));
        }
        if (!Utils.isNullOrBlank(params.getString("section_id")))
        {
            linkParam.append("&section_id=" + params.getString("section_id"));
        }

        if (!Utils.isNullOrBlank(params.getString("date")))
        {
            linkParam.append("&date=" + params.getString("date"));
        }

        return linkParam.toString();
    }

    /**
     * 专家详情查询
     * 
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ModelAndView searchDocInfo(HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        Row param = this.parseRequestParametersToRow(request);
        Map<String, Object> attrs = new HashMap<String, Object>();
        Row docInfo = this.getDoctorDelegate().getDocInfo(param);

        attrs.put("docInfo", docInfo); // 这一句相当于request.setAttribute("list",list);
        return new ModelAndView("doctor", attrs);
    }
}
